#!/bin/sh


# $1 - full path to source directory (directory itself not copied)
# $2 - full path to desstination directory
# $3 - wildcard or file/dir name
function DirectoryCopySubset()
{
	echo "DirectoryCopySubset [$1]/[$3] to [$2]" 2>&1 | tee -a "$logFile"
	(cd "$1"; tar cf - "./$3") | (cd "$2"; tar xfv -)
}

# $1 - full path to source directory (directory itself not copied)
# $2 - full path to desstination directory
function DirectoryCopyContents()
{
	echo "DirectoryCopyContents [$1] to [$2]" 2>&1 | tee -a "$logFile"
	(cd "$1"; tar cf - *) | (cd "$2"; tar xfv -)
}

# $1 - full path to source directory (directory itself not removed)
# $2 - wildcard or file/dir name
function DirectoryRemoveSubset()
{
	echo "DirectoryRemoveSubset [$2] from [$1]" 2>&1 | tee -a "$logFile"
	find -d "$1" -name "$2" -execdir rm -rfv {} \;	
}


# echo "postflight script may be replaced by BAS."

logFile="/var/log/ScanThru Installer Log File.txt"
echo "###########  ScanThru post-installer ###########" 2>&1 | tee -a "$logFile"

thisScriptPath=$0
packagePath=$1
targetLocation=$2
targetVolume=$3
thisScriptDirectory=`dirname "$thisScriptPath"`

TIME_BEGIN=`date`
echo "### ScanThru DS=$bundleName START: $TIME_BEGIN" 2>&1 | tee -a "$logFile"


# check user permissions
_uid=`/usr/bin/id -u`
if [ "$_uid" != "0" ]
then
	echo "" 2>&1 | tee -a "$logFile"
	echo "You should have root privileges in order to install ScanThru." 2>&1 | tee -a "$logFile"
	echo "Please login as root or use an utility 'su' or 'sudo' when you run $thisScriptPath." 2>&1 | tee -a "$logFile"
	echo "Current UID: $_uid." 2>&1 | tee -a "$logFile"
	exit 2
else
	echo "root UID: $_uid." 2>&1 | tee -a "$logFile"
fi


twainDirectory="/Library/Image Capture/TWAIN Data Sources"
if [ ! -d "$twainDirectory" ] ; then
	echo "TWAIN DS directory is missing: $twainDirectory!" 2>&1 | tee -a "$logFile"
	exit 1
else
	echo "TWAIN DS directory is found: $twainDirectory ." 2>&1 | tee -a "$logFile"
fi

packageResourcesDir="$thisScriptDirectory/1"
if [ ! -d "$packageResourcesDir" ] ; then
	echo "package resources directory is missing: $packageResourcesDir!" 2>&1 | tee -a "$logFile"
	exit 1
else
	echo "package resources directory is found: $packageResourcesDir ." 2>&1 | tee -a "$logFile"
fi


#  read bundle name from Info.plist
bundleName=`cat "$packageResourcesDir/Info.plist" | grep CFBundleName -A1 | tail -1 | sed 's.*<string>' | sed 's</string>' | awk '{print $0}' | tr -d "\r"`

if [ -z $bundleName ] ; then
	echo "bundle name is not determined!" 2>&1 | tee -a "$logFile"
	exit 1
else
	echo "bundle name is determined: $bundleName ." 2>&1 | tee -a "$logFile"
fi

bundlePath="$twainDirectory/$bundleName"
bundleResourcesDir="$bundlePath/Contents/Resources"

# rename generic bundle to product-specific name
if [ -d "$bundlePath" ] ; then
	echo "duplicate package is found: $bundlePath, removing" 2>&1 | tee -a "$logFile"
	DirectoryRemoveSubset "$twainDirectory" "$bundleName"
fi

mv "$twainDirectory/OEMDS.ds" "$bundlePath"


# copy product-specific components
DirectoryCopyContents "$packageResourcesDir" "$bundleResourcesDir"
if [ "$?" -ne 0 ]
then
	echo "copying * from $packageResourcesDir to $bundleResourcesDir failed!" 2>&1 | tee -a "$logFile"
	exit 1
else
	echo "copying * from $packageResourcesDir to $bundleResourcesDir succeeded." 2>&1 | tee -a "$logFile"
fi

# copy Info.plist
mv -f "$bundleResourcesDir/Info.plist" "$bundlePath/Contents/Info.plist" 2>&1 | tee -a "$logFile"

# set permissions
# echo "chown -R root $bundlePath/" 2>&1 | tee -a "$logFile"
chown -R root "$bundlePath/" 2>&1 | tee -a "$logFile"
# echo "chgrp -R admin $bundlePath/" 2>&1 | tee -a "$logFile"
chgrp -R admin "$bundlePath/" 2>&1 | tee -a "$logFile"

find "$bundlePath" -type f -exec chmod 644 {} \;
# 2>&1 | tee -a "$logFile"
find "$bundlePath" -type d -exec chmod 755 {} \;
# 2>&1 | tee -a "$logFile"
find "$bundlePath/Contents/MacOS" -exec chmod 755 {} \;
#  2>&1 | tee -a "$logFile"
chmod 755 "$bundlePath" 2>&1 | tee -a "$logFile"

TIME_END=`date`
echo "### ScanThru DS=$bundleName END: $TIME_END" 2>&1 | tee -a "$logFile"
